<?php defined('SYSPATH') or die('No direct access allowed.');

class Model_Supplier_Product_Number extends ORM
{
    /**
     * @param $params
     * @return array
     * 设置总库存
     */
    public static function set_product_number($params)
    {
        $table_field = St_Functions::table_field($params['type_id']);
        $product_id = DB::select()->from($table_field[0])->where("id={$params['suit_id']}")->execute()->get($table_field[1]);
        $day_num = (strtotime($params['end_time'])-strtotime($params['start_time']))/86400+1;
        $to_day = strtotime($params['start_time']);

        $insert_arr = array();
        $update_arr = array(
            'number' => "number = CASE id ",
        );
        $update_id = array();

        for ($i=0;$i<$day_num;$i++){
            $to_day = $i>0?$to_day+86400:$to_day;
            $is_exist = DB::select()->from($table_field[4])->where("suit_id={$params['suit_id']} and day={$to_day}")->execute()->current();
            if($is_exist){
                array_push($update_id,$is_exist['id']);
                switch ($params['number_rule'])
                {
                    case 1:
                        $update_arr['number'] .= "WHEN {$is_exist['id']} THEN '{$params['number']}' ";
                        break;
                    case 2:
                        $week = date('w',$to_day)>0?date('w',$to_day):7;
                        if(in_array($week,$params['week_val'])){
                            $update_arr['number'] .= "WHEN {$is_exist['id']} THEN '{$params['number']}' ";
                        }
                        break;
                    case 3:
                        $month = date('j',$to_day);
                        if(in_array($month,$params['month_val'])){
                            $update_arr['number'] .= "WHEN {$is_exist['id']} THEN '{$params['number']}' ";
                        }
                        break;
                }
            }else{
                $insert_sql = '';
                switch ($params['number_rule'])
                {
                    case 1:
                        $insert_sql = "('{$product_id}','{$params['suit_id']}','{$to_day}','{$params['number']}')";
                        break;
                    case 2:
                        $week = date('w',$to_day)>0?date('w',$to_day):7;
                        if(in_array($week,$params['week_val'])){
                            $insert_sql = "('{$product_id}','{$params['suit_id']}','{$to_day}','{$params['number']}')";
                        }
                        break;
                    case 3:
                        $month = date('j',$to_day);
                        if(in_array($month,$params['month_val'])){
                            $insert_sql = "('{$product_id}','{$params['suit_id']}','{$to_day}','{$params['number']}')";
                        }
                        break;
                }
                if($insert_sql){
                    array_push($insert_arr,$insert_sql);
                }else{
                    continue;
                }
            }
        }
        $result = false;
        if(count($insert_arr)>0){
            $insert_sql = "INSERT INTO sline_{$table_field[4]}(`product_id`,`suit_id`,`day`,`number`) VALUES ";
            $insert_sql.= implode(',',$insert_arr);
            $result = DB::query(1, $insert_sql)->execute();
        }
        if(count($update_id)>0){
            $update_sql = "UPDATE sline_{$table_field[4]} SET ";
            $update_id = implode(',',$update_id);
            foreach ($update_arr as $k=>$v){
                $update_sql.= $v."ELSE {$k} END,";
            }
            $update_sql = substr($update_sql,0,strlen($update_sql)-1);
            $update_sql.= " WHERE id IN({$update_id}) ";
            $result = DB::query(1, $update_sql)->execute();
        }

        return array(
            'status' => $result || $result===0?true:false,
            'msg'    => $result || $result===0?'设置成功':'设置失败',
        );
    }

    /**
     * @param $params
     * @return array
     * 修改对应日期的库存
     */
    public static function set_product_single_number($params)
    {
        $table_field = St_Functions::table_field($params['type_id']);
        $day = strtotime($params['date']);
        $result = DB::update($table_field[4])->set(array('number'=>$params['number']))->where("suit_id={$params['suit_id']} and day={$day}")->execute();
        if($table_field[6] && $params['number']!='-1'){
            $result = DB::update($table_field[6])->set(array('number'=>$params['number']))->where("{$table_field[5]}={$params['suit_id']} and day={$day}")->execute();
        }
        return array(
            'status' => $result || $result===0?true:false,
            'msg'    => $result || $result===0?'设置成功':'设置失败',
            'number' => $params['number'],
            'date'   => $params['date'],
        );
    }
}